function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds();


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * 封装console.log
 */
function print(key, ...data) {
  console.log(key + '-->', data);
}

function getCurMonth() {
  var date = new Date();
  var year = date.getFullYear(); //得到当前年份
  var month = date.getMonth() + 1; //得到当前月份，getMonth（）从0-11 ，所以此处需加1
  month = month < 10 ? "0" + month : month;
  return year + "-" + month;
}

function getNextMonth() {
  var date = new Date();
  var year = date.getFullYear(); //得到当前年份
  var month = date.getMonth() + 1; //得到当前月份，getMonth（）从0-11 ，所以此处需加1
  if (month == 12) {
    year = year + 1;
    month = 1;
  }
  month++;
  month = month < 10 ? "0" + month : month;
  return year + "-" + month;
}


function timestampToTime(timestamp) {
  var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = date.getDate() + ' ';
  var hour = date.getHours();
  var h = (hour < 10 ? '0' + hour : hour) + ':';
  var minute = date.getMinutes();
  var m = (minute < 10 ? '0' + minute : minute) + ':';
  var s = date.getSeconds();
  return Y + M + D + h + m + (s < 10 ? '0' + s : s);
}

function getStatisticMonthDays(year, month) {
  var date = new Date();
  var curMonth = date.getMonth() + 1; //得到当前月份，getMonth（）从0-11 ，所以此处需加1
  var days = 30;
  if (curMonth != month) {
    if (month == 2) {
      if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        days = 29;
      } else {
        days = 28;
      }
    } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
      days = 31;
    } else {
      days = 30;
    }
  } else {
    days = date.getDate();
  }
  return days;
}

/**
 * 判断小程序是否支持免押
 */
function isCouldNoDeposit(systemInfo) {

  var baseVer = systemInfo.SDKVersion;
  var osType = systemInfo.system.toLowerCase();
  var sysVer = systemInfo.version;
  var baseVerFlg = compareVer('2.0.7', baseVer);
  if (!baseVerFlg) {
    return isCouldNoDepositSysVer(osType, sysVer, baseVer);
  }
  return true;
}
/**
 * 系统是否支持免押
 * true支持，fase不支持
 */
function isCouldNoDepositSysVer(osType, sysVer, baseVer) {

  var baseFlg = compareVer('1.3.0', baseVer);
  if (!baseFlg) {
    return false;
  }
  if (osType.indexOf("android") != -1) {
    return compareVer('6.5.10', sysVer)
  }
  if (osType == 'ios') {
    return compareVer('6.5.9', sysVer)
  }
}

function compareVer(lowVer, highVer) {
  var lowVerArr = lowVer.split(".");
  var highVerArr = highVer.split(".");
  var lowFirV = parseInt(lowVerArr[0]);
  var highFirV = parseInt(highVerArr[0]);
  if (lowFirV < highFirV) {
    return true;
  } else if (lowFirV == highFirV) {

    var lowSecV = lowVerArr.length > 1 ? parseInt(lowVerArr[1]) : 0;
    var highSecV = highVerArr.length > 1 ? parseInt(highVerArr[1]) : 0;

    if (lowSecV < highSecV) {
      return true;
    } else if (lowSecV == highSecV) {
      var lowThirV = lowVerArr.length > 2 ? parseInt(lowVerArr[2]) : 0;
      var highThirV = highVerArr.length > 2 ? parseInt(highVerArr[2]) : 0;
      if (lowThirV <= highThirV) {
        return true;
      }
    }
  }

  return false;
}

function mp_record_user_action(BASE_URL, adId, tipPage, action) {
  let userId = wx.getStorageSync('user_id')
  getApp().executeRequest(function (header) {
    wx.request({
      url: BASE_URL + '/weixin/optTipAd',
      headers: header,
      data: {
        userId: userId,
        adId: adId, //广告id，对应广告标题
        miniPro: 'wx', //wx：微信小程序；ali：支付宝小程序
        tipPage: tipPage, //rent：租借；back：归还
        action: action //read：展示；success：成功跳转；fail：取消跳转--仅小程序跳转有fail
      },
      success: function (res) {
        console.log("【关注公众号】日志记录成功..." + action)
      },
      fail: function (res) {
        console.log("【关注公众号】日志记录失败..." + action)
      }
    })
  })
}

function bee_record_user_action(BASE_URL, adId, tipPage, action) {
  let userId = wx.getStorageSync('user_id')
  getApp().executeRequest(function (header) {
    wx.request({
      url: BASE_URL + '/weixin/optTipAd',
      headers: header,
      data: {
        userId: userId,
        adId: adId, //广告id，对应广告标题
        miniPro: 'wx', //wx：微信小程序；ali：支付宝小程序
        tipPage: tipPage, //rent：租借；back：归还
        action: action //read：展示；success：成功跳转；fail：取消跳转--仅小程序跳转有fail
      },
      success: function (res) {
        console.log("【小蜜蜂广告记录】日志记录成功..." + action)
      },
      fail: function (res) {
        console.log("【小蜜蜂广告记录】日志记录失败..." + action)
      }
    })
  })
}

/**
 * 小蜜蜂页面UV记录
 * @param {string} name 页面名称
 * @param {string} url 页面地址
 */
function bee_record_page_uv(name, url) {
  if (getApp().globalData.miniBeeConfig && getApp().globalData.miniBeeConfig.uv && getApp().globalData.miniBeeConfig.uv.active == 1) {
    console.log('【小蜜蜂UV记录】已开启UV记录功能...');
    wx.request({
      url: getApp().globalData.miniBeeConfig.uv.url,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        name: name,
        url: url,
      },
      success(res) {
        console.log('【小蜜蜂UV记录】请求成功：' + JSON.stringify(res));
      },
      fail(error) {
        console.log('【小蜜蜂UV记录】请求失败：' + JSON.stringify(error));
      }
    })
  } else {
    console.log('【小蜜蜂UV记录】未开启UV记录功能...');
  }
}
/**
 * 复制内容到粘贴面板
 * @param {string} text 内容
 */
function copyContent(text){
  wx.setClipboardData({ //设置系统剪贴板的内容
    data: text,
    success(res) {
      wx.getClipboardData({ // 获取系统剪贴板的内容
        success(res) {
          wx.showToast({
            title: '复制成功',
            duration:1500
          })
        }
      })
    }
  })
}
module.exports = {
  formatTime: formatTime,
  getCurMonth: getCurMonth,
  getNextMonth: getNextMonth,
  timestampToTime: timestampToTime,
  getStatisticMonthDays: getStatisticMonthDays,
  isCouldNoDeposit: isCouldNoDeposit,
  print: print,
  bee_record_page_uv: bee_record_page_uv,
  bee_record_user_action: bee_record_user_action,
  mp_record_user_action: mp_record_user_action,
  copyContent:copyContent
}